var $pForm = $("#pForm");
var $brId = $("#brId");
var $proId = $("#proId");
var $order = $("#order");
var $pageN = $("#pageN");
var $brNav = $("#brNav ul li a");
var $attr = $(".class-attr ul li a");
var $sort = $(".class-order a");
var $startPrice = $("#startPrice");
var $endPrice = $("#endPrice");

$brNav.click(function() {
	var $this = $(this);
	if ($this.hasClass("curr")) {
		$brId.val("");
	} else {
		$brId.val($this.attr("brId"));
	}
	$pageN.val(1);
	$pForm.submit();
	return false;
});

$attr.click(function() {
	var $this = $(this);
	if ($this.hasClass("curr")) {
		$this.closest("nav").find("input").prop("disabled", true);
	} else {
		$this.closest("nav").find("input").prop("disabled", false).val(
				$this.text());
	}
	$pageN.val(1);
	$pForm.submit();
	return false;
});

$sort.click(function() {
	var $this = $(this);
	if ($this.hasClass("curr")) {
		if ($order.val() == "priceAsc") {
			$order.val("priceDesc");
		} else {
			$order.val("priceAsc");
		}
	} else {
		$order.val($this.attr("order"));
	}
	$pageN.val(1);
	$pForm.submit();
	return false;
});

$pForm.submit(function() {
	if ($brId.val() == "") {
		$brId.prop("disabled", true)
	}
	if ($proId.val() == "") {
		$proId.prop("disabled", true)
	}
	if ($order.val() == "" || $order.val() == "topDesc") {
		$order.prop("disabled", true)
	}
	if ($pageN.val() == "" || $pageN.val() == "1") {
		$pageN.prop("disabled", true)
	}
	if ($startPrice.val() == "" || !/^\d+(\.\d+)?$/.test($startPrice.val())) {
		$startPrice.prop("disabled", true)
	}
	if ($endPrice.val() == "" || !/^\d+(\.\d+)?$/.test($endPrice.val())) {
		$endPrice.prop("disabled", true)
	}
});

$.pageSkip = function(pageN) {
	$pageN.val(pageN);
	$pForm.submit();
	return false;
};

var keyArray = $('#search-bar').val().split('');
var keyword = "";
for (var i = 0; i < keyArray.length; i++) {
	keyword = keyword + keyArray[i] + " ";
}
keyword = $.trim(keyword);
function highlight(pucl) {
	if ("" == keyword)
		return;
	var temp = pucl.html();
	var htmlReg = new RegExp("\<.*?\>", "i");
	var arrA = [];
	// 替换HTML标签
	for (var i = 0; true; i++) {
		var m = htmlReg.exec(temp);
		if (m) {
			arrA[i] = m;
		} else {
			break;
		}
		temp = temp.replace(m, "{[(" + i + ")]}");
	}
	words = unescape(keyword.replace(/\+/g, ' ')).split(/\s+/);
	// 替换关键字
	for (w = 0; w < words.length; w++) {
		var r = new RegExp("("
				+ words[w].replace(/[(){}.+*?^$|\\\[\]]/g, "\\$&") + ")", "ig");
		temp = temp.replace(r, "<b>$1</b>");
	}
	// 恢复HTML标签
	for (var i = 0; i < arrA.length; i++) {
		temp = temp.replace("{[(" + i + ")]}", arrA[i]);
	}
	pucl.html(temp);

}

var searchKeys ;
$.event.special.valuechange = {

  teardown: function (namespaces) {
    $(this).unbind('.valuechange');
  },

  handler: function (e) {
    $.event.special.valuechange.triggerChanged($(this));
  },

  add: function (obj) {
    $(this).on('keyup.valuechange cut.valuechange paste.valuechange input.valuechange', obj.selector, $.event.special.valuechange.handler)
  },

  triggerChanged: function (element) {
    var current = element[0].contentEditable === 'true' ? element.html() : element.val()
      , previous = typeof element.data('previous') === 'undefined' ? element[0].defaultValue : element.data('previous');
    if (current !== previous) {
      element.trigger('valuechange', [element.data('previous')]);
      element.data('previous', current)
    }
  }
};
$(function () {
    $('#search-bar').on('valuechange', function (e, previous) {
        var k =$(this).val();
    	var ajax = $.ajax({
            type: "GET",
            url: "/p/sugg.htm",
            data: {keyword:k},
            dataType: "text",
            success: function(data){
            	searchKeys=data.split(",");
            	show(searchKeys);
            }
        });
      })
});
function show(searchKeys){
	 var nhl = $.map(searchKeys, function (team) {
   		return {
   			  value: team, data: {  }
   			  }; 
    	});
	 $('#search-bar').devbridgeAutocomplete({
	      lookup: nhl,
	      minChars: 1,
	      onSelect: function (suggestion) {
	      },
	      showNoSuggestionNotice: false
	 });
	 $('#search-bar').focus();
}
